package com.example.fragment.tool;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class DataBaseTool {
    private static SQLiteDatabase db;
    public static MyDataBaseHelper helper;
    private static final String TABLE_NAME2 = "Test";
    private static final String TABLE_NAME3 = "Person";
    private static final String TABLE_NAME1 = "Plan1";

    public static void madeobject(Context context,  int version){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
    }
    public static void add(Context context,  int version,String a,String b,String c,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        //使用 ContentValues 来对要添加的数据进行组装
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("a1",a);
        values.put("a2",b);
        values.put("a3",c);
        // 插入第一条数据
        db.insert(tablename,null,values);
        values.clear();
        //退出程序后，关闭数据库资源
        db.close();
    }
    public static void add(Context context,  int version,String a,String b,String c,String d,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名

        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        //使用 ContentValues 来对要添加的数据进行组装
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("a1",a);
        values.put("a2",b);
        values.put("a3",c);
        values.put("a4",d);
        // 插入第一条数据
        db.insert(tablename,null,values);
        values.clear();
        //退出程序后，关闭数据库资源
        db.close();
    }
    public static void add(Context context,  int version,String a,String b,String c,String d,String e,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        //使用 ContentValues 来对要添加的数据进行组装
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("a1",a);
        values.put("a2",b);
        values.put("a3",c);
        values.put("a4",d);
        values.put("a5",e);
        // 插入第一条数据
        db.insert(tablename,null,values);
        values.clear();
        //退出程序后，关闭数据库资源
        db.close();
    }
    public static void add(Context context,  int version,String a,String b,String c,String d,String e,String f,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        //使用 ContentValues 来对要添加的数据进行组装
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("a1",a);
        values.put("a2",b);
        values.put("a3",c);
        values.put("a4",d);
        values.put("a5",e);
        values.put("a6",f);
        // 插入第一条数据
        db.insert(tablename,null,values);
        values.clear();
        //退出程序后，关闭数据库资源
        db.close();
    }
    public static void add(Context context,  int version,String a,String b,String c,String d,String e,String f,String g,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        //使用 ContentValues 来对要添加的数据进行组装
        ContentValues values = new ContentValues();
        // 开始组装第一条数据
        values.put("a1",a);
        values.put("a2",b);
        values.put("a3",c);
        values.put("a4",d);
        values.put("a5",e);
        values.put("a6",f);
        values.put("a6",g);
        // 插入第一条数据
        db.insert(tablename,null,values);
        values.clear();
        //退出程序后，关闭数据库资源
        db.close();
    }
    public static void delete(Context context,  int version,String tablename,int deleted){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        db.delete(tablename,"id=?",new String[]{String.valueOf(deleted)});
        db.close();
    }
    //一次取出7列
    public static ArrayList<ArrayList<String>> getlist7(Context context,  int version,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        //构造数据
        ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
        do {
            ArrayList<String> mRowData = new ArrayList<String>();
            String a1 = cursor.getString(cursor.getColumnIndex("a1"));
            String a2 = cursor.getString(cursor.getColumnIndex("a2"));
            String a3 = cursor.getString(cursor.getColumnIndex("a3"));
            String a4 = cursor.getString(cursor.getColumnIndex("a4"));
            String a5 = cursor.getString(cursor.getColumnIndex("a5"));
            String a6 = cursor.getString(cursor.getColumnIndex("a6"));
            String a7 = cursor.getString(cursor.getColumnIndex("a7"));
            if(a1!= null){
                mRowData.add(a1);
                mRowData.add(a2);
                mRowData.add(a3);
                mRowData.add(a4);
                mRowData.add(a5);
                mRowData.add(a6);
                mRowData.add(a7);
                mTableDatas.add(mRowData);
               }else {System.out.println("啊没有数据");}

        } while (cursor.moveToNext());
        db.close();
        return mTableDatas;

    }
    //一次取出6列
    public static ArrayList<ArrayList<String>> getlist6(Context context,  int version,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        //构造数据
        ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
        do {
            ArrayList<String> mRowData = new ArrayList<String>();
            String a1 = cursor.getString(cursor.getColumnIndex("a1"));
            String a2 = cursor.getString(cursor.getColumnIndex("a2"));
            String a3 = cursor.getString(cursor.getColumnIndex("a3"));
            String a4 = cursor.getString(cursor.getColumnIndex("a4"));
            String a5 = cursor.getString(cursor.getColumnIndex("a5"));
            String a6 = cursor.getString(cursor.getColumnIndex("a6"));
            if(a1!= null){
                mRowData.add(a1);
                mRowData.add(a2);
                mRowData.add(a3);
                mRowData.add(a4);
                mRowData.add(a5);
                mRowData.add(a6);
                mTableDatas.add(mRowData);
            }else {System.out.println("啊没有数据");}

        } while (cursor.moveToNext());
        db.close();
        return mTableDatas;
    }
    //一次取出5列
    public static ArrayList<ArrayList<String>> getlist5(Context context,  int version,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        //构造数据
        ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
        do {
            ArrayList<String> mRowData = new ArrayList<String>();
            String a1 = cursor.getString(cursor.getColumnIndex("a1"));
            String a2 = cursor.getString(cursor.getColumnIndex("a2"));
            String a3 = cursor.getString(cursor.getColumnIndex("a3"));
            String a4 = cursor.getString(cursor.getColumnIndex("a4"));
            String a5 = cursor.getString(cursor.getColumnIndex("a5"));
            if(a1!= null){
                mRowData.add(a1);
                mRowData.add(a2);
                mRowData.add(a3);
                mRowData.add(a4);
                mRowData.add(a5);
                mTableDatas.add(mRowData);
            }else {System.out.println("啊没有数据");}

        } while (cursor.moveToNext());
        db.close();
        return mTableDatas;
    }
    //一次取出4列
    public static ArrayList<ArrayList<String>> getlist4(Context context,  int version,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        //构造数据
        ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
        do {
            ArrayList<String> mRowData = new ArrayList<String>();
            String a1 = cursor.getString(cursor.getColumnIndex("a1"));
            String a2 = cursor.getString(cursor.getColumnIndex("a2"));
            String a3 = cursor.getString(cursor.getColumnIndex("a3"));
            String a4 = cursor.getString(cursor.getColumnIndex("a4"));
            if(a1!= null){
                mRowData.add(a1);
                mRowData.add(a2);
                mRowData.add(a3);
                mRowData.add(a4);
                mTableDatas.add(mRowData);
            }else {System.out.println("啊没有数据");}

        } while (cursor.moveToNext());
        db.close();
        return mTableDatas;
    }
    //一次取出3列
    public static ArrayList<ArrayList<String>> getlist3(Context context,  int version,String tablename){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        //构造数据
        ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
        do {
            ArrayList<String> mRowData = new ArrayList<String>();
            String a1 = cursor.getString(cursor.getColumnIndex("a1"));
            String a2 = cursor.getString(cursor.getColumnIndex("a2"));
            String a3 = cursor.getString(cursor.getColumnIndex("a3"));

            if(a1!= null){
                mRowData.add(a1);
                mRowData.add(a2);
                mRowData.add(a3);

                mTableDatas.add(mRowData);
            }else {System.out.println("啊没有数据");}

        } while (cursor.moveToNext());
        db.close();
        return mTableDatas;
    }
    //得到这一行的id
    public static ArrayList<Integer> getid(Context context,  int version,String tablename){
        ArrayList<Integer> datalist =new ArrayList();
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        helper.getWritableDatabase();//建立数据库
        db = helper.getWritableDatabase();
        //创建游标
        Cursor cursor = db.query(tablename,null,null,null,null,null,null);
        //游标置顶
        cursor.moveToFirst();
        do {
            int  id = cursor.getInt(cursor.getColumnIndex("id"));
            datalist.add(id);
        } while (cursor.moveToNext());
        db.close();
        return datalist;
    }
    public static void changdata(Context context,int version,  String tablename,int id,String a1,String a2,String a3,String a4,String a5,String a6,String a7){
        MyDataBaseHelper helper = new MyDataBaseHelper(context, "dbName", null, version);//dbName数据库名
        //建立数据库
        db = helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("a1",a1);
        values.put("a2",a2);
        values.put("a3",a3);
        values.put("a4",a4);
        values.put("a5",a5);
        values.put("a6",a6);
        values.put("a7",a7);
        db.update(tablename,values,"id=?",new String[]{String.valueOf(id)});
        db.close();
    }


}
